<?php

/**
 * SQLITE类
 * mer.shanghai@gmail.com
 *
 */

class Sqlite {

  var $link       = NULL;
  var $config     = array();
  var $version    = '';
  var $querynum  = 0;

  function __construct($config=array()) {
    $this->config['dbhost']   = 'sqlite.db';
    $this->config['dbuser']   = '';
    $this->config['dbpw']     = '';
    $this->config['dbname']   = '';
    $this->config['charset']  = 'utf8';
    $this->config['pconnect'] = 0;
    $this->config['dbpre']    = 'fsdkp_';

    $this->setconfig($config);
  }

  function __destruct() {
    $this->close();
  }

  function setconfig($config=array()) {
    if (!empty($config) && is_array($config)){
      foreach($config as $key=>$value) {
        $this->config[$key] = $value;
      }
    }
  }

  function connect($dbhost, $pconnect=0) {
    $func = $pconnect==0 ? 'sqlite_open' : 'sqlite_popen';
    $this->link = @$func($dbhost, 0666, $error) or die("Could not connect to databse");
    $this->version = sqlite_libversion();
    return $this->link;
  }

  function close() {
    return sqlite_close($this->link);
  }

  function get($key) {
    return $this->config[$key];
  }
  
  function query($sql, $type='') {
    $sql = trim(str_replace('@_@', $this->config['dbpre'], $sql));
    if ($this->link === NULL) {
      $this->connect($this->config['dbhost'], $this->config['pconnect']);
    }
    $func = $type == 'UNBUFFERED' ? 'sqlite_unbuffered_query' : 'sqlite_query';
    if (preg_match("/^\s*SELECT/i", $sql)) {
      $query = $func($this->link, $sql, SQLITE_ASSOC, $error);
    }
    else {
      $query = sqlite_exec($this->link, $sql, $error);
    }
    $this->querynum++;
    return $query;
  }

  function getOne($sql, $type='', $result_type=SQLITE_ASSOC) {
    $query = $this->query($sql, $type);
    return sqlite_fetch_array($query, $result_type);
  }

  function getAll($sql, $type='', $result_type=SQLITE_ASSOC) {
    $query = $this->query($sql, $type);
    $result = array();
    while($ret = sqlite_fetch_array($query, $result_type)) {
      $result[] = $ret;
    }
    return $result;
  }

  function insert_id() {
    return sqlite_last_insert_rowid($this->link);
  }

  function result($query, $row) {
    return @sqlite_fetch_all($query, SQLITE_ASSOC);
  }

  function affected_rows() {
    return sqlite_changes($this->link);
  }

  function num_rows($query) {
    return sqlite_num_rows($query);
  }

  function num_fields($query) {
    return sqlite_num_fields($query);
  }

  function free_result($query) {
    return;
  }

  function querynum() {
    return $this->querynum;
  }


  function error() {
    return sqlite_error_string($this->errno());
  }

  function errno() {
    return sqlite_last_error($this->link);
  }


  function arr_insert($arr) {
    if (is_array($arr)) {
      $fstr = '';
      $vstr = '';
      foreach ($arr as $key => $val) {
        $fstr .= '"' . $key . '", ';
        $vstr .= '\'' . $val . '\', ';
      }
      if ($fstr) {
        $fstr = ' (' . substr($fstr, 0, -2) . ') ';
        $vstr = ' (' . substr($vstr, 0, -2) . ') ';
        return $fstr . ' VALUES ' . $vstr;
      }
      else {
        return ' ';
      }
    }
    else {
      return ' ';
    }
  }

  function arr_update($arr) {
    if (is_array($arr)) {
      $ustr = '';
      foreach ($arr as $key => $val) {
        $ustr .= '"' . $key . '" = \'' . $val . '\', ';
      }
      return $ustr ? ' ' . substr($ustr, 0, -2) . ' ' : ' ';
    }
    else {
      return '';
    }
  }


  function fetch_array($query, $result_type=SQLITE_ASSOC) {
    return sqlite_fetch_array($query, $result_type);
  }

  function fetch_row($query) {
    return sqlite_fetch_array($query, SQLITE_NUM);
  }

  function fetch_assoc($query) {
    return $this->fetch_array($query, SQLITE_ASSOC);
  }


  function do_page($sql, $pagenumber=1, $pagesize=12) {
    $start = ($pagenumber-1) * $pagesize;
    $sql  .= ' LIMIT ' . $start . ', ' . $pagesize;
    return $this->getAll($sql);
  }

  function do_navi($sql, $pagenumber=1, $pagesize=12) {
    $total = $this->num_rows($this->query($sql));
    $pages = ceil($total / $pagesize);
    return array(
      total => $total,
      pages => $pages,
      page  => $pagenumber,
      pagesize => $pagesize
    );
  }

  function pagination($sql, $pagenumber=1, $pagesize=12) {
    $page = (int)$pagenumber;
    $size = (int)$pagesize;
    $page = $page>0 ? $page : 1;
    $size = $size>0 ? $size : 12;
    return array(
      $this->do_navi($sql,$page,$size),
      $this->do_page($sql,$page,$size)
    );
  }

  
  function backup() {
    return $this->config['dbhost'];
  }

}

?>